/* nixonFTP
 * FTP client version 0.1
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
*/

package nixonftp.ui;
import nixonftp.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToggleButton;
import javax.swing.ListCellRenderer;
import javax.swing.SpringLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import nixonftp.list.NXDefaultListModel;
import nixonftp.list.NXQueueModel;
import nixonftp.list.NXQueueRenderer;
import nixonftp.list.NXTableModel;


public class NXQueuePanel extends JPanel {
	private final JTable list;
	private NXQueueModel listModel;
	private JLabel lblProgress;
	private final JLabel lblTime;
	private NXQueueItem currentItem;
	private final JLabel lblTotalProgress;
	private final JLabel lblTotalTime;
	private JScrollPane listScroll;
	private JToggleButton tool;
	private JPanel panel;
	private JLabel lblAll;

	public JButton btnStopAll;
	public JButton btnStop;
	public JToggleButton btnPauseAll;
	SpringLayout layout;
	private final JLabel lblCurrent;
	private final NXQueueRenderer renderer;

	public NXQueuePanel() {
		super();
		setLayout(new BorderLayout());
		panel = new JPanel(new GridBagLayout()); //top panel

		JLabel l;
		GridBagConstraints c = new GridBagConstraints();
		lblCurrent = new JLabel("Current");
		Font f = lblCurrent.getFont().deriveFont(Float.valueOf("14")).deriveFont(Font.BOLD);
		lblCurrent.setFont(f);
		Insets defaultInsets = new Insets(2,12,2,12);
		c.gridx = 0;
		c.gridy = 3;
		c.insets = defaultInsets;
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		panel.add(lblCurrent, c);

		c.gridy = 0;
		lblAll = new JLabel("All");
		lblAll.setFont(f);
		panel.add(lblAll, c);

		c.gridx = 0;
		c.insets = defaultInsets;
		c.anchor = GridBagConstraints.FIRST_LINE_START;

		lblProgress = new JLabel("0 bytes");
		Font smallFont = lblProgress.getFont().deriveFont(11F);
		lblProgress.setFont(smallFont);
		c.gridy = 4;
		panel.add(lblProgress, c);
		
		lblTime = new JLabel("0 seconds");
		lblTime.setFont(smallFont);
		c.gridy = 5;
		panel.add(lblTime, c);

		c.gridy = 1;
		lblTotalProgress = new JLabel("0 bytes");
		lblTotalProgress.setFont(smallFont);
		panel.add(lblTotalProgress, c);

		lblTotalTime = new JLabel("0 seconds");
		lblTotalTime.setFont(smallFont);
		c.gridy = 2;
		panel.add(lblTotalTime, c);

		c.gridx = 1;

		c.gridy = 0;
		btnPauseAll = new JToggleButton(NXIcon.pause);
		panel.add(btnPauseAll, c);

		c.gridy = 1;
		btnStopAll = new JButton(NXIcon.cancel);
		panel.add(btnStopAll, c);

		c.gridy = 4;
		btnStop = new JButton(NXIcon.cancel);
		panel.add(btnStop, c);

		add(panel, BorderLayout.NORTH);

		list = new JTable() {
			public TableCellRenderer getCellRenderer(int row, int column) {
				return renderer;
			}
		}; //queue
		listScroll = new JScrollPane(list);
		add(listScroll, BorderLayout.CENTER);
		
		listModel = new NXQueueModel();
		list.setModel(listModel);
		renderer = new NXQueueRenderer(true);
		//list.setPrototypeCellValue(new NXQueueItem("index", "", "/", ""));

		list.getColumnModel().getColumn(0).setHeaderValue("");
		list.getColumnModel().getColumn(1).setHeaderValue("Item");
		list.getColumnModel().getColumn(2).setHeaderValue("Size");
		updateActivity(null);
	}

	public void addItem(NXQueueItem item, int pos) {
		if (pos == -1) {
			listModel.addRow(item);
		} else {
			listModel.insertRow(pos, item);
		}
		list.invalidate();
	}

	public NXQueueItem getItem(NXQueueItem item) {
		return (NXQueueItem) listModel.getValueAt(listModel.indexOf(item));
	}

	public void removeItem(NXQueueItem item) {
		listModel.removeRowAt(listModel.indexOf(item));
		list.invalidate();
	}

	public void removeAllItems() {
		listModel.removeAll();
		list.invalidate();
	}

	public void updateActivity(NXQueueItem item) {
		boolean show = item != null;
		if (show) {
			String activity = "Unknown";

			if (item.activity.equals("list")) {
				activity = "Directory Listing";
			} else {
				activity = item.activity;
			}
		}
		/*lblProgress.setVisible(show);
		lblTime.setVisible(show);
		lblTotalProgress.setVisible(show);
		lblTotalTime.setVisible(show);
		lblAll.setVisible(show);
		lblCurrent.setVisible(show);*/
		currentItem = item;
	}

	public void updateProgress(long currentProgress, int currentSeconds, long totalTransferred, int totalSeconds, long totalSize) {
		if (currentItem == null) return;
		lblProgress.setText(NXFormat.fileSize(currentProgress) + " / " + NXFormat.fileSize(currentItem.totalSize));

		int bps = 0;
		if (currentSeconds != 0) bps = (int) (currentProgress / currentSeconds);
		
		int eta = NXFormat.getEta(currentItem.totalSize, currentProgress, bps);
		lblTime.setText(NXFormat.parseTime(currentSeconds) + NXFormat.eta(eta));
		//if (currentSeconds != 0) lblSpeed.setText(NXFormat.fileSize(bps, true) + "/sec");

		lblTotalProgress.setText(NXFormat.fileSize(totalTransferred) + " / " + NXFormat.fileSize(totalSize));

		if (totalSeconds != 0) bps = (int) (totalTransferred / totalSeconds);
		else bps = 0;
		eta = NXFormat.getEta(totalSize, totalTransferred, bps);
		lblTotalTime.setText(NXFormat.parseTime(totalSeconds));
		if (totalSeconds != 0) lblAll.setText("All at " + NXFormat.fileSize(bps) + "/s");
	}
}
class QueueCellRenderer extends JPanel implements ListCellRenderer {

	public Component getListCellRendererComponent(JList list,
		Object value,
		int index,
		boolean isSelected,
		boolean cellHasFocus) {
		removeAll();

		NXQueueItem queueItem = (NXQueueItem) value;
		JLabel lblActivity = new JLabel(queueItem.activity + " " + queueItem.fileName);
		JLabel lblSize = new JLabel(NXFormat.fileSize(queueItem.totalSize, true));

		add(lblActivity);
		add(lblSize);

		return this;
	}
}
